<template>
  <div class="login">

    <!--中间登录-->
    <div class="login-box">
      <!--头像-->
      <div class="avatar-box">
        <img src="../../assets/logo.png" alt="">
      </div>

      <!--form表单-->
      <div class="form-box">
        <el-form ref="loginFormRef"
                 :model="loginForm"
                 :rules="loginFormRules"
                 label-width="0">
          <!--用户名，属性方式icon-->
          <el-form-item prop="username">
            <el-input v-model="loginForm.username"
                      placeholder="请输入用户名"
                      prefix-icon="el-input__icon iconfont icon-user"></el-input>
          </el-form-item>
          <!--密码，slot方式-icon-->
          <!--el-icon-sunny  el-icon-moon-->
          <el-form-item prop="password">
            <el-input v-model="loginForm.password"
                      :type="showPasswordFlag?'':'password'"
                      placeholder="请输入密码">
              <i slot="suffix" :class="getPasswordIcon" @click="showPassword"></i>
              <!--<i slot="suffix" class="el-input__icon el-icon-sunny"></i>-->
              <i slot="prefix" class="iconfont icon-3702mima"></i>
            </el-input>
          </el-form-item>
          <!--按钮-->
          <el-form-item class="btn-container">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'index',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名' },
          { min: 3, max: 12, message: '用户名长度为3-12位' }
        ],
        password: [
          { required: true, message: '请输入密码' }
        ]
      },
      showPasswordFlag: false
    }
  }, // data结束
  computed: {
    getPasswordIcon () {
      return [
        'el-input__icon',
        this.showPasswordFlag ? 'el-icon-sunny' : 'el-icon-moon'
      ]
    }
  },
  methods: {
    // 切换密码的显示和隐藏
    showPassword () {
      this.showPasswordFlag = !this.showPasswordFlag
    },
    // 登录
    login () {
      console.log('loginForm:', this.loginForm)
      this.$refs.loginFormRef.validate(valid => {
        console.log('是否校验通过：', valid)
        if (!valid) return false
        console.log('调用接口')
      })
    },
    // 重置
    reset () {
      this.$refs.loginFormRef.resetFields()
    }
  }, // methods结束
  created () {

  } // created结束
}
</script>

<style scoped lang="less">
  .login {
    background: #2b4b6b;
    height: 100%;
    position: relative;
    .login-box {
      width: 450px;
      height: 300px;
      background: #FFF;
      border-radius: 3px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      /*头像*/
      .avatar-box {
        padding: 10px;
        border-radius: 50%;
        background: #FFF;
        position: absolute;
        top: 0;
        left: 50%;
        transform: translate(-50%, -50%);
        /*border: 1px solid #ccc;*/
        box-shadow: 0 0 10px #ddd;
        img {
          width: 130px;
          height: 130px;
          display: block;
          background: #EEE;
          border-radius: 50%;
        }
      }
      // 表单
      .form-box {
        /*padding: 100px 20px 0;*/
        position: absolute;
        bottom: 0;
        width: 100%;
        .el-form {
          padding: 0 20px;
        }
        .btn-container {
          /*text-align: right;*/
          display: flex;
          justify-content: flex-end;
        }
      }
    }
  }
</style>
